package com.youxin.cpcenter.service.impl;

import com.youxin.busfoundation.bean.classify.RequestClassifyIndustryBean;
import com.youxin.busfoundation.bean.classify.ResponseCityBean;
import com.youxin.busfoundation.bean.classify.ResponseClassifyBean;
import com.youxin.busfoundation.bean.ent.QueryEntMemberBean;
import com.youxin.busfoundation.bean.ent.RequestCompanyInfoBean;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.bean.phone.QueryPhoneLatestTime;
import com.youxin.busfoundation.bean.region.RequestRegionBean;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.common.es.*;
import com.youxin.busfoundation.common.es.bean.*;
import com.youxin.busfoundation.common.name.NameOperator;
import com.youxin.busfoundation.utils.security.AesUtils;
import com.youxin.cpcenter.dao.TbEntMemberInfoMapper;
import com.youxin.cpcenter.service.*;
import com.youxin.foundation.utils.ArrayUtils;
import com.youxin.foundation.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc 公司接口实现
 * @author Kris.wang
 * @date 2071
 */
@Service("searchCompanyService")
public class SearchCompanyServiceImpl extends BaseServiceImpl implements SearchCompanyService {

	@Autowired
	private ClassifyService classifyService;

	@Autowired
	private RegionService regionService;

	@Autowired
	private ViewLeadsService viewLeadsService;

	@Autowired
	private LeadsService leadsService;

	@Autowired
	private TbEntMemberInfoMapper tbEntMemberInfoMapper;

	/**
	 * @desc 企业联系人接口
	 * @param requestBean
	 * @return
	 * @throws Exception
	 */
	public List<PortraitContactBean> searchPortraitContact(RequestContactBean requestBean) throws Exception {
		UserInfo userInfo = LoginAuth.getUserInfo();
		List<PortraitContactBean> list = new SearchContact<PortraitContactBean>().queryContact(requestBean,
				PortraitContactBean.class);
		if (!ArrayUtils.isEmpty(list)) {
			QueryEntMemberBean queryEntMemberBean = new QueryEntMemberBean();
			queryEntMemberBean.setCompanyUid(requestBean.getCompanyUid());
			List<String> mobiles = list.stream().filter((t) -> !StringUtils.isNullOrEmpty(t.getMobile())).map((t) -> {
				return t.getMobile();
			}).collect(Collectors.toList());

			for (PortraitContactBean item : list) {
				item.setName(item.getName());
			}

			if (!ArrayUtils.isEmpty(mobiles)) {
				queryEntMemberBean.setMobiles(mobiles);
				List<String> queryResults = tbEntMemberInfoMapper.selectByCompanyUidAndMobiles(queryEntMemberBean);
				for (PortraitContactBean item : list) {
					/**
					 * 不是企业版成员
					 */
					if (!queryResults.contains(item.getMobile())) {
						String name = NameOperator.hideName(item.getName());
						item.setName(name);
						String position = NameOperator.hidePosition(item.getTitle());
						item.setTitle(position);
					}
				}
				QueryPhoneLatestTime latestTimeBean = new QueryPhoneLatestTime();
				latestTimeBean.setCallPhone(userInfo.getPhone());
				latestTimeBean.setPhones(mobiles);
			}
			for (PortraitContactBean t : list) {
				try {
					if (!StringUtils.isNullOrEmpty(t.getMobile())) {
						String mobile = AesUtils.encrypt(t.getMobile(), String.valueOf(userInfo.getId()));
						t.setMobile(mobile);
					}
				} catch (Exception e) {
				}

				try {
					if (!StringUtils.isNullOrEmpty(t.getTelephone())) {
						String telphone = AesUtils.encrypt(t.getTelephone(), String.valueOf(userInfo.getId()));
						t.setTelephone(telphone);
					}
				} catch (Exception e) {
				}
			}
		}
		return list;
	}

	/**
	 * @desc 企业资源接口
	 * @param requestBean
	 * @return
	 * @throws Exception
	 */
	public List<PortraitResourceBean> searchPortraitResource(RequestResourceBean requestBean) throws Exception {
		UserInfo userInfo = LoginAuth.getUserInfo();
		List<PortraitResourceBean> portraitResourceBean = new SearchResource<PortraitResourceBean>()
				.queryResource(requestBean, PortraitResourceBean.class);

		// 格式化数据结构
		for (PortraitResourceBean item : portraitResourceBean) {
			// 行业
			if (item.getIndustry() != null) {
				RequestClassifyIndustryBean requestClassifyIndustryBean = new RequestClassifyIndustryBean();
				requestClassifyIndustryBean.setUids(item.getIndustry());
				List<ResponseClassifyBean> list = classifyService.selectByUids(requestClassifyIndustryBean);
				List<String> industryName = new ArrayList<String>();
				for (ResponseClassifyBean i : list) {
					industryName.add(i.getName());
				}
				item.setIndustryName(industryName);
			}

			// 区域
			if (item.getArea() != null) {
				RequestRegionBean requestRegionBean = new RequestRegionBean();
				requestRegionBean.setUids(item.getArea());
				List<ResponseCityBean> list = regionService.selectByRegionIds(requestRegionBean);
				List<String> areaName = new ArrayList<String>();
				for (ResponseCityBean i : list) {
					areaName.add(i.getRegionName());
				}
				item.setAreaName(areaName);
			}

			// 联系人
			if (!StringUtils.isNullOrEmpty(item.getMemberUid())) {
				ContactHandle contactHandle = new ContactHandle();
				ContactBean contactBean = contactHandle.getContactIndex(item.getMemberUid());
				if (contactBean != null) {
					item.setEmail(contactBean.getEmail());
					// 手机
					if (!StringUtils.isNullOrEmpty(contactBean.getMobile())) {
						item.setMobile(AesUtils.encrypt(contactBean.getMobile(), String.valueOf(userInfo.getId())));
					} else {
						item.setMobile("");
					}
					// 电话号码
					if (!StringUtils.isNullOrEmpty(contactBean.getTelephone())) {
						item.setTelephone(
								AesUtils.encrypt(contactBean.getTelephone(), String.valueOf(userInfo.getId())));
					} else {
						item.setTelephone("");
					}
					item.setLogoUrl("");
					item.setName(contactBean.getName());
					item.setPosition(contactBean.getTitle());
					item.setCoopPurposes("");
					item.setCoopMethods("");
				}
			}
		}

		return portraitResourceBean;
	}

	/**
	 * @desc 企业产品和服务接口
	 * @param requestBean
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PortraitProductServiceBean> searchPortraitProductService(RequestProductServiceBean requestBean) {
		try {
			CompanyBaseBean companyBaseBean = null;
			RequestCompanyInfoBean bean = new RequestCompanyInfoBean();
			bean.setUid(requestBean.getCompanyUid());
			String[] attr = { "product_service" };
			companyBaseBean = queryCompanyInfo(bean, attr);
			if (companyBaseBean.getProductService() != null) {
				return companyBaseBean.getProductService();
			}
		} catch (Exception e) {
		}
		return new ArrayList<PortraitProductServiceBean>();
	}

	/**
	 * @desc 企业画像-基本信息
	 * @param requestBean
	 * @param include
	 * @return
	 * @throws Exception
	 */
	public CompanyBaseBean queryCompanyInfo(RequestCompanyInfoBean requestBean, String[] include) throws Exception {
		return new SearchCompanyInfo().queryCompanyInfo(requestBean, include);
	}

	/**
	 * @desc 根据企业UIDs批量获取企业
	 * @param uids
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CompanyBaseBean> queryCompanyInfos(List<String> uids) throws Exception {
		return new SearchCompanyInfo().queryCompanyInfos(uids);
	}

}
